package com.first.agent.service.impl;

import com.first.agent.common.JsonResult;
import com.first.agent.common.R;
import com.first.agent.dao.UserManagementDao;
import com.first.agent.dao.impl.UserManagementDaoImpl;
import com.first.agent.pojo.AgentDetails;
import com.first.agent.pojo.AgentDetailsJ;
import com.first.agent.pojo.User;
import com.first.agent.pojo.UserJ;
import com.first.agent.service.UserManagementService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserManagementServiceImpl implements UserManagementService {
    UserManagementDao userManagementDao = new UserManagementDaoImpl();
    @Override
    public JsonResult<List> findAll(int page, int limit) {
        JsonResult<List> r = new JsonResult<>();
        try {
            List<UserJ> all = userManagementDao.findAll(page, limit);
            int count = userManagementDao.findCount();
            if(all!=null){
                //登录成功
                r.setCode(0);
                r.setMsg("");
                r.setData(all);
                r.setCount(count);
            }else{
                r.setCode(500);
                r.setMsg("后台异常!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println(r);
        return r;
    }

    @Override
    public R<UserJ> findUser(int id) {
        UserJ userJ = null;
        R r = new R();
        try {
            userJ = userManagementDao.findUser(id);
            if(userJ!=null){
                r.setCode(0);
                r.setMsg("");
                r.setData(userJ);
            }else {
                r.setCode(500);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    //删除一条记录
    @Override
    public R delUser(int id) {
        int i = 0;
        R r = new R();
        try {
            i = userManagementDao.delUser(id);
            if (i==0){
                r.setCode(400);
                r.setMsg("删除失败！");
            }else{
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public int findRoleIdByRole(String role) {
        int id = -1;
        try {
            id = userManagementDao.findRoleIdByRole(role);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return id;
    }

    @Override
    public R modifyById(User user) {
        R r = new R();
        try {
            int i = userManagementDao.modifyById(user);
            if(i==1){
                r.setCode(200);
                r.setMsg("修改成功！");
            }else {
                r.setCode(500);
                r.setMsg("修改失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public JsonResult<List> findAgentDetail(String username) {
        List<AgentDetails> list = null;
        JsonResult<List> jsonResult = new JsonResult<>();
        try {
            list = userManagementDao.findAgentDetail(username);
            if(list!=null){
                jsonResult.setCode(0);
                jsonResult.setMsg("");
                jsonResult.setData(list);
            }else {
                jsonResult.setCode(500);
                jsonResult.setMsg("后台异常！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return jsonResult;
    }

    @Override
    public List<Map<String, Object>> findAllRole() {
        List<Map<String, Object>> list = null;
        try {
            list = userManagementDao.findAllRole();
            System.out.println(list);
            System.out.println("================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public R addUser(User user) {
        R r = new R();
        try {
            int i = userManagementDao.addUser(user);
            if (i==1){
                r.setCode(200);
                r.setMsg("添加成功！");
            }else{
                r.setCode(500);
                r.setMsg("添加失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public R findAllYu(String username,int page, int limit) {
        R r = new R();
        try {
            List<AgentDetailsJ> allYu = userManagementDao.findAllYu(username,page, limit);
            System.out.println(allYu);
            if (allYu!=null){
                r.setCode(0);
                r.setData(allYu);
            }else {
                r.setMsg("后台出错！");
                r.setCode(500);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return r;
    }
}
